Skip to content

Allow show to write the screenshot to a file-like object - #2136

Open
lucas-santos-f wants to merge 1 commit into
CadQuery:masterfrom
lucas-santos-f:png-to-memory
Open

lucas-santos-f wants to merge 1 commit into
CadQuery:masterfrom
lucas-santos-f:png-to-memory

Conversation

@lucas-santos-f

Copy link
Copy Markdown

Fixes #1760

show(..., screenshot=...) now also takes a binary file-like object such as io.BytesIO. In that case vtkPNGWriter writes to memory (WriteToMemoryOn) and the PNG bytes are written to the object, so docs or notebooks can get the image without going through a temporary file:

from io import BytesIO

buf = BytesIO()
show(b, screenshot=buf, interact=False)
png = buf.getvalue()

A file name works as before. The check is hasattr(screenshot, "write") rather than isinstance(screenshot, str) because VTK also takes a pathlib.Path in SetFileName, and that should keep working. The example above is added to the Screenshots section of doc/vis.rst.

The new test_screenshot_to_memory uses the patch_vtk fixture, but feeds the real vtkPNGWriter a 10x5 image from vtkImageCanvasSource2D in place of the window, and checks the PNG signature and the size in the IHDR chunk. On master it fails with TypeError: SetFileName argument 1.

Checked locally (Windows 10, Python 3.12, pip install with cadquery-ocp 8.0.1.1, VTK 9.6.2):

  • off-screen render of a box at 320x240: the bytes in the BytesIO are identical to the file written with screenshot="img.png"
  • pytest -n 8: 904 passed, 3 skipped (master: 903 passed, 3 skipped), no new warnings
  • black . --diff --check (cq fork): nothing to reformat
  • mypy cadquery: same errors as master; here they are almost all import-untyped for OCP, since the pip package has no stubs
  • docs build with Sphinx 9.1.0: no new warnings, and the new example shows up under Screenshots

Happy to adjust anything if needed, e.g. if you'd rather have show return the bytes.

AI disclosure: this change was written with the help of Claude Code (Claude Opus 5.5).

screenshot now also takes a binary file-like object such as io.BytesIO.
The PNG is then written to memory with vtkPNGWriter.WriteToMemoryOn and
copied into it, which avoids a temporary file when the image is only
needed in memory (docs, notebooks).

Fixes CadQuery#1760
@codecov

codecov Bot commented Nov 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.81%. Comparing base (b3f9afd) to head (645a2af).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2136   +/-   ##
=======================================
  Coverage   95.81%   95.81%           
=======================================
  Files          30       30           
  Lines        9612     9616    +4     
  Branches     1446     1447    +1     
=======================================
+ Hits         9210     9214    +4     
  Misses        247      247           
  Partials      155      155           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Write PNG to io.Bytes

1 participant